
(import
 (scheme r5rs)
 (aop 2)
 (aop 3)
 (aop 12))

(define (exercise-12.1)
  (define acc-max (accumulator-maker 0 max))
  (for-each (lambda (n)
              (send acc-max 'update! n))
            '(3 7 2 4 10 1 5))
  (send acc-max 'show))

(define (exercise-12.2)
  (define (double-box-maker item1 item2)
    (let ((left-box (box-maker item1))
          (right-box (box-maker item2)))
      (lambda msg
        (case (first msg)
          ((show-left) (send left-box 'show))
          ((show-right) (send right-box 'show))
          ((update-left!)
           (send left-box 'update! (second msg)))
          ((update-right!)
           (send right-box 'update! (second msg)))
          ((reset!)
           (for-effect-only
            (begin
              (set! left-box item1)
              (set! right-box item2))))))))
  (define a (double-box-maker 11 21))
  (send a 'show-left))

(define (word-frequency-maker)
  (let ((naive-hash-function
         (lambda (s)
           (remainder (char->integer (string-ref s 0)) 26))))
    (let ((h (hash-table-maker 26 naive-hash-function)))
      (lambda (string-list)
        (for-each
         (lambda (s) (send h 'update! s add1 (lambda (s) 1)))
         string-list)
        h))))

(define test-sample '("this" "hash" "table" "is" "as" "inefficient"
                      "as" "a" "bucket" "we" "chose" "the" "vector"
                      "too" "small" "and" "we" "chose" "the"
                      "hash" "function" "too" "naively" "of" "course"
                      "this" "would" "never" "be" "done" "in" "practice"
                      "most" "systems" "discourage" "the" "user" "from"
                      "worrying" "about" "the" "size" "of" "the"
                      "hash" "table" "or" "the" "nature" "of" "the"
                      "hash" "function"))

(define (exercise-12.22)
  (define word-frequency (word-frequency-maker))
  (word-frequency test-sample))

